package com.fenghongzhang.day005;

import android.Manifest;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

public class MainActivity extends RxAppCompatActivity {
    private CirclePercentView circleView;
    private Button btn;
    private static final String TAG = "MainActivity";
    private RxPermissions rxPermissions = new RxPermissions(this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        circleView = (CirclePercentView) findViewById(R.id.circleView);
        btn = (Button) findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int n = (int)(Math.random()*100);
                circleView.setPercent(n);
            }
        });

        //都问
        rxPermissions.request(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.READ_SMS})
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        Log.i(TAG, "accept: "+aBoolean);
                        if (!aBoolean){
                            finish();
                        }
                    }
                });
        //询问每一个
//        rxPermissions.requestEach(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.READ_SMS})
//                .subscribe(new Consumer<Permission>() {
//                    @Override
//                    public void accept(Permission permission) throws Exception {
//                        if(permission.granted){
//                            Log.i(TAG, "accept: 同意");
//                        }else if(permission.shouldShowRequestPermissionRationale){
//                            Log.i(TAG, "accept: 不同意");
//                        }else{
//                            Log.i(TAG, "accept: 一直不同意");
//                        }
//                    }
//                });
        


    }

    public void btn(View view) {

        Observable.interval(1, TimeUnit.SECONDS)
                .compose(this.<Long>bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new Consumer<Long>() {
            @Override
            public void accept(Long aLong) throws Exception {
                Log.i(TAG, "accept: "+aLong);
            }
        });

    }
}
